#ifndef LOGIC_H
#define LOGIC_H
#ifdef __cplusplus

extern "C"{

#endif
#include "stm32l1xx_hal.h"
//#define SHORT_DEBUG

#define M_FLASH_ADDR 0X08000000+60*1024
#define ST_LIMIT_VALUE 2000
#define SRC_ADDR (*((uint16_t*)&reg8[1]))
#define ADC_VALE (*(uint16_t*)&reg8[3])
#define PLUSE_COUNT (*(uint16_t*)&reg8[5])
#define BIT0(X) (X&0X01)
#define BIT1(X) ((X>>1)&0X01)
#define BIT2(X) ((X>>2)&0X01)
#define BIT3(X) ((X>>3)&0X01)
#define BIT4(X) ((X>>4)&0X01)
#define BIT5(X) ((X>>5)&0X01)
#define BIT6(X) ((X>>6)&0X01)
#define BIT7(X) ((X>>7)&0X01)

extern uint8_t  reg8[32];

typedef enum adcState
{
    ADC_WORK_e = 0,
    ADC_FINISH_e,
    ADC_WAIT_e
}adcState;

typedef enum STATE_t
{
    INTERRUPT_FROM_ADC = 0,
    INTERRUPT_FROM_LORA,
    INTERRUPT_FROM_LORA_PIN,
    INTERRUPT_FROM_12HTIMER,
    INTERRUPT_FROM_24HTIMER,
    INTERRUPT_FROM_10M1,
    INTERRUPT_FROM_10M2,
    INTERRUPT_FROM_10M3,
    LORA_REPORT,
    SLEEP
}STATE;
extern RTC_HandleTypeDef hrtc;
extern UART_HandleTypeDef huart1;
extern ADC_HandleTypeDef hadc;

extern uint16_t adc_value[1000];
extern uint16_t adc_store[500];
extern uint16_t gAdcCount;
extern uint8_t u1Len;
extern uint8_t u1Buff[256];
extern STATE gState;
extern STATE lastState;
extern uint8_t gInt;
extern uint8_t gMode;
extern uint8_t gWaitMode;
extern RTC_DateTypeDef gData;
extern RTC_TimeTypeDef gTime;
extern RTC_TimeTypeDef gNextTime;
extern uint8_t gSrcAddr;
extern uint16_t gAdcValue;
extern uint16_t gBtValue;
extern adcState gAdcState;
extern uint8_t  gSendFlag;
extern uint8_t lastIoState ; //default up
extern uint16_t lastValue ;
void Logic_Data_Deal(void);
uint16_t Power_ADC_Read(uint32_t timeout);
void Set_Rtc_Time(void);
void Get_Rtc_Time(void);
void Set_AlarmA_Time(uint8_t perHour,uint8_t perMin,uint8_t perSec);
void Set_AlarmB_Time(uint8_t perHour,uint8_t perMin,uint8_t perSec);
void Cycle_Scure_Report(void);
void Scan_MulBtn(void);
void Enter_Stop_Mode(void);
void Exit_Stop_Mode(void);
void RTCA_WakeUp_Callback(void);
void RTCB_WakeUp_Callback(void);
void RTC_Switch_RTCA(void);
void RTC_Switch_RTCB_24H(void);
void LORA_WakeUp_Callback(void);
void ADC_WakeUp_Callback(void);
void ADC_Sample_Cycle(void);
void Logic_Data_Deal(void);
void Set_Exti_Pin_Analog(void);
void Set_Exti_Pin_Interrupt(void);
void Print_Time(void);
#ifdef __cplusplus

};

#endif
#endif
